package com.coocaa.internship.service.serviceImpl;

import com.coocaa.internship.common.ResultCode;
import com.coocaa.internship.common.ResultData;
import com.coocaa.internship.dao.UserLoginDao;
import com.coocaa.internship.pojo.UserLogin;
import com.coocaa.internship.service.UserLoginService;
import com.coocaa.internship.util.ParamvalidatorUtil;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Author: zwh
 * @Date: 2019-07-27
 * @Time: 22:04
 */

@Service()
public class UserLoginServiceImpl implements UserLoginService {

    @Autowired
    UserLoginDao userLoginDao;

    @Autowired
    SMSservicelmpl smSservicelmpl;
    @Override
    public boolean checkUserById(Long userId) {
        ParamvalidatorUtil.paramValidate(userId);
        UserLogin userLogin = userLoginDao.selectUserLoginById(userId);
        if (userLogin != null) {
            return true;
        }
        return false;
    }


    @Override
    public ResultData LoginByPassword(@NonNull UserLogin userLogin) {
        ResultData resultData = new ResultData();
        userLogin = (UserLogin) userLoginDao.selectUserLoginByPass(userLogin);
        if (userLogin != null) {
            resultData.setCode(ResultCode.SUCCESS);
            resultData.setData(userLogin);
        } else
            resultData.setCode(ResultCode.FAILED);
        return resultData;
    }

    @Override
    public boolean LoginByValidateCode(@NonNull UserLogin userLogin, @NonNull int validatecode) {
        if (smSservicelmpl.CodeValidate(String.valueOf(validatecode), userLogin.getUserPhone())) {
            if (userLoginDao.selectUserLoginByPhone(userLogin) == null) {
                userLoginDao.insertUserLoginByPhone(userLogin);
            }
            return true;
        } else {
            return false;
        }
    }
}
